package com.rsxy.service.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@Api(tags= "统一错误处理相关接口")
public class MyErrorController implements ErrorController {

    @GetMapping("/error")
    @ApiOperation(value = "页面出错捕捉",notes = "测试数据")
    public ModelAndView handleError(HttpServletRequest request){
        //获取statusCode:401,404,500
        ModelAndView mv=new ModelAndView();
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if(statusCode == 500){
            mv.setViewName("../page/template/error/error-500");

        }else if(statusCode == 404){
            //对应的是/error/404.html、/error/404.jsp等，文件位于/templates下面
            mv.setViewName("page/template/error/error-404");
        }else if(statusCode == 403){
            mv.setViewName("page/template/error/error-403");
        }else{
            mv.setViewName("page/template/error/error-500");
        }
         return mv;
    }


    @Override
    public String getErrorPath() {
        return "/error";
    }
}